<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="岗位名称" prop="postName">
        <el-input
            v-model="queryParams.postName"
            placeholder="请输入岗位名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="dataTable" v-loading="loading" :row-key="getRowKey" :data="postList" @select="handSelect" @select-all="handleMultiplePostSelect">
      <el-table-column type="selection" :reserve-selection="true" width="50" align="center" />
      <el-table-column label="岗位编号" align="center" prop="postId" />
      <el-table-column label="岗位编码" align="center" prop="postCode" />
      <el-table-column label="岗位名称" align="center" prop="postName" />
      <el-table-column label="岗位排序" align="center" prop="postSort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page-sizes="[5,10]"
      layout="prev, pager, next"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {StrUtil} from "@/utils/StrUtil";
import { postList } from '@/api/flowable/definition'

export default {
  name: "FlowPost",
  dicts: ['sys_normal_disable'],
  // 接受父组件的值
  props: {
    // 回显数据传值
    selectData: {
      type: Array,
      default: () => [],
      required: false
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        roleName: undefined,
        roleKey: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      selectedRows: [] // 存储选中的行数据
    };
  },
  created() {
    this.getList();
    this.selectedRows = this.selectData?.map(item => ({
      postId: item.id ?? item.postId,
      postName: item.name ?? item.postName
    })) ?? [];
    this.selectDataChange();
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      postList(this.queryParams).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.$nextTick(() => {
            // 打开时回显数据
            this.echoChooseItems();
          });
        }
      );
    },
    // 选中数据变化
    selectDataChange() {
      this.$emit('handleAssignSelect', this.selectedRows);
    },
    // 回显（勾选）已选事项
    echoChooseItems() {
      this.selectedRows.forEach(selectRow => {
        const index = this.postList.findIndex(item => item.postId.toString() === selectRow.postId.toString());
        if (index !== -1) {
          this.$refs.dataTable.toggleRowSelection(this.postList[index], true);
        }
      });
    },
    // 单选框选中数据
    handSelect(selection, row) {
      if (this.selectedRows.findIndex(selectRow => selectRow.postId.toString() === row.postId.toString()) < 0) {
        this.selectedRows.push(row);
      } else {
        // 回显数据里有本条，把这条删除(取消选中)
        this.selectedRows.forEach((selectRow, index) => {
          if (selectRow.postId.toString() === row.postId.toString()) {
            this.selectedRows.splice(index, 1);
          }
        });
      }
      this.$emit('handleAssignSelect', this.selectedRows);
    },
    // 全选数据
    handleMultiplePostSelect(selection) {
      if (selection.length > 0) {
        selection.forEach((item) => {
          if (!this.selectedRows.includes(item.postId.toString())) {
            this.selectedRows.push(item);
          }
        });
      } else {
        this.postList.forEach((item) => {
          this.selectedRows.forEach((selectRow, index) => {
            if (selectRow.postId.toString() === item.postId.toString()) {
              this.selectedRows.splice(index, 1);
            }
          });
        });
      }
      this.$emit('handleAssignSelect', this.selectedRows);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.handleQuery();
    },
    // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
    getRowKey(row) {
      return row.postId
    },
  }
};
</script>
<style>
/*隐藏radio展示的label及本身自带的样式*/
/*.el-radio__label{*/
/*  display:none;*/
/*}*/
</style>
